package com.sdyeda.readiot.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


public class HexTools {

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }
	
	private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();

	public static String bytesToHex(byte[] bytes) {
		char[] hexChars = new char[bytes.length * 2];
		for (int j = 0; j < bytes.length; j++) {
			int v = bytes[j] & 0xFF;
			hexChars[j * 2] = HEX_ARRAY[v >>> 4];
			hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
		}
		return new String(hexChars);
	}
	
	public static String bytesToHexString(byte[] bytes,int start,int length) {
		byte[] result = new byte[length];
		System.arraycopy(bytes, start, result, 0, length);
		return bytesToHex(result);
	}
	
	public static int byte2ToInt(byte[] by ) {
		byte[] high= new byte[2];
		if(by.length ==2)
		{
			if((by[0] & 0x80) == 0x80) {  //负数
				high[0]=high[1]=(byte)0xFF;
			}
			else{  
				high[0]=high[1]=(byte)0;
			}
		}
		return (high[0]&0xFF)<<24 
				|(high[1]&0xFF)<<16
				|(by[0]&0xFF)<<8
				|(by[1]&0xFF);
	}
	
	public static byte[] getBytesTimeBCD() {
		//byte time[] = new byte[6];

		Calendar date = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss",Locale.CHINA);
		return HexTools.hexStringToByteArray(sdf.format(date.getTimeInMillis()));

		/*
		
		time[0] = (byte) (date.get(Calendar.YEAR) - 2000);
		// time[0] += time[0] / 10 * 6;
		time[1] = (byte) (date.get(Calendar.MONTH)+1);
		// time[1] += time[1] / 10 * 6;
		time[2] = (byte) date.get(Calendar.DAY_OF_MONTH);
		// time[2] += time[2] / 10 * 6;
		time[3] = (byte) date.get(Calendar.HOUR_OF_DAY);
		// time[3] += time[3] / 10 * 6;
		time[4] = (byte) date.get(Calendar.MINUTE);
		// time[4] += time[4] / 10 * 6;
		time[5] = (byte) date.get(Calendar.SECOND);
		// time[5] += time[5] / 10 * 6;

		return time;
		*/
	}
	
	
	public static byte[] getBytesRainTimeBCD() {
		//byte time[] = new byte[6];

		Calendar date = Calendar.getInstance();
		Calendar date1 = Calendar.getInstance();
		
		if(date.get(Calendar.HOUR_OF_DAY) ==0) {
			date.add(Calendar.HOUR_OF_DAY, -1);
			date1.add(Calendar.HOUR_OF_DAY, -1);
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd",Locale.CHINA);
		
		return HexTools.hexStringToByteArray(sdf.format(date.getTimeInMillis())+sdf.format(date1.getTimeInMillis()));
/*
		time[0] = (byte) (date.get(Calendar.YEAR) - 2000);
		// time[0] += time[0] / 10 * 6;
		time[1] = (byte) (date.get(Calendar.MONTH)+1);
		// time[1] += time[1] / 10 * 6;
		time[2] = (byte) (date.get(Calendar.DAY_OF_MONTH));
		// time[2] += time[2] / 10 * 6;
		time[3] = (byte) (date1.get(Calendar.YEAR) - 2000);
		// time[3] += time[3] / 10 * 6;
		time[4] = (byte) (date1.get(Calendar.MONTH)+1);
		// time[4] += time[4] / 10 * 6;
		time[5] = (byte) (date1.get(Calendar.DAY_OF_MONTH));
		// time[5] += time[5] / 10 * 6;

		return time;
		*/
	}
	
	public static Calendar BytesBCDtoTime(byte[] bsdtime) {

		Calendar time = Calendar.getInstance();

		time.set(bsdtime[0] + 2000, bsdtime[1], bsdtime[2], bsdtime[3], bsdtime[4], bsdtime[5]);
		return time;
	}
	
	public static Date BytesBCDtoDate(byte[] bsdtime) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss",Locale.CHINA);
		try {
			return sdf.parse(bytesToHex(bsdtime));
			//return sdf.parse(String.format("%4d-%2d-%2d %2d:%2d:%2d",
				//	bsdtime[0] + 2000, bsdtime[1], bsdtime[2], bsdtime[3], bsdtime[4], bsdtime[5] ));
		} catch (ParseException e) {
			return new Date();
		}
	}
}
